package dao;

import bean.Vip;
import util.Dbconnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class VipDao {
    public static List<Vip> selvip() {

        List<Vip> list = new ArrayList<Vip>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = Dbconnection.getcConnection();
            String sql = "select * from vip";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Vip sho = new Vip();
                sho.setNum(rs.getString("num"));
                sho.setName(rs.getString("name"));
                sho.setGender(rs.getInt("gender"));
                sho.setAge(rs.getInt("age"));
                sho.setOnum(rs.getInt("onum"));
                list.add(sho);
                System.out.println(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbconnection.close(conn, ps, rs);
        }

        return list;
    }

    public static void del(String id) {
        Connection conn = null;
        Statement stat = null;
        int rs;
        String sql = "delete from vip where num='" + id + "'";
        try {
            conn = Dbconnection.getcConnection();
            stat = conn.createStatement();
            rs = stat.executeUpdate(sql);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbconnection.close(conn, stat, null);
        }

      //  return false;
    }

    public static void insert(String no, String name, int time, int address, int type) {

            Connection conn = null;
            PreparedStatement ps = null;

            try {
                conn = Dbconnection.getcConnection();
                String sql = "insert into vip (num,name,gender,age,onum) value(?,?,?,?,?)";
                ps = conn.prepareStatement(sql);
                ps.setString(1, no);
                ps.setString(2, name);
                ps.setInt(3, time);
                ps.setInt(4, address);
                ps.setInt(5, type);
                ps.executeUpdate();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                Dbconnection.close(conn, ps, null);
            }

    }

    public static Vip upda(String id) {
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        String sql = "select * from vip where num='" + id + "'";

        try {
            conn = Dbconnection.getcConnection();
            stat = conn.prepareStatement(sql);
            rs = stat.executeQuery(sql);
            while (rs.next()) {
                Vip shop = new Vip();
                shop.setId(rs.getInt("id"));
                shop.setNum(rs.getString("num"));
                shop.setName(rs.getString("name"));
                shop.setGender(rs.getInt("gender"));
                shop.setAge(rs.getInt("age"));
                shop.setOnum(rs.getInt("onum"));
                return shop;

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbconnection.close(conn, stat, null);
        }
        return null;

    }

    public void upda1(Vip shop) {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean isok = false;
        try {
            conn = Dbconnection.getcConnection();
            String sql = "update vip set num=?,name=?,gender=?,age=?,onum=? where id=?";
            System.out.println(sql);
            ps = conn.prepareStatement(sql);
            ps.setString(1, shop.getNum());
            ps.setString(2, shop.getName());
            ps.setInt(3, shop.getGender());
            ps.setInt(4, shop.getAge());
            ps.setInt(5, shop.getOnum());
            ps.setInt(6, shop.getId());
            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbconnection.close(conn, ps, null);
        }
      //  return isok;
    }
}
